---
title: "synthetic_Italy_2024"
output: html_document
date: "2024-02-25"
---
---
title: "analysis_imf"
output: html_document
date: "2023-06-29"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## R Markdown
```{r}
library(readr)
library(ggplot2)
library(ggcorrplot)
library(tidysynth)
```

```{r}
data <- read_csv("dta_20240225.csv")
```

## show x
```{r}
data2 <- data %>%
  select(-Country, -period, -target) %>%
  na.omit()
```




```{r}
cor_matrix <- cor(data2, use = "complete.obs")
CorResult <- cor(data2, method="pearson")
PCorResult <- cor_pmat(data2)
ggcorrplot(corr = CorResult, method = "square", title = "TEST p-value PLOT",
           type = "lower", p.mat = PCorResult, insig = "pch")
```
```{r}
par(mfrow = c(ceiling(sqrt(ncol(data2))), ceiling(sqrt(ncol(data2)))))
par(mar = c(2, 2, 2, 2))
for (col in colnames(data3)) {
  if (is.numeric(data2[[col]])) {
    hist(data2[[col]], main = col, xlab = col, ylab = "Frequency")
  }
}

```


```{r}
# Shapiro-Wilk
shapiro_results <- lapply(data2, function(x) {
  if (is.numeric(x)) {
    result <- shapiro.test(x)
    list(W = result$statistic, p_value = result$p.value)
  } else {
    NULL
  }
})


for (i in 1:length(shapiro_results)) {
  if (!is.null(shapiro_results[[i]])) {
    cat("Variable:", names(shapiro_results)[i], "\n")
    cat("W:", shapiro_results[[i]]$W, "\n")
    cat("p-value:", shapiro_results[[i]]$p_value, "\n")
    if (shapiro_results[[i]]$p_value < 0.05) {
      cat("The variable is not normally distributed.\n")
    } else {
      cat("The variable is normally distributed.\n")
    }
    cat("\n")
  }
}
```
## show y

```{r}
g <- ggplot() +
  geom_line(data, mapping = aes(x = period, y = government_trust, group = Country, color = target)) +
  geom_vline(xintercept = 75, linetype = "dashed") +
  scale_color_manual(values = c("black", "grey")) +
  scale_x_continuous(breaks = seq(62,92,2),
                     labels=c("2004","2005", "2006", "2007", "2008",  "2009",  "2010",  "2011",  "2012",  "2013",  "2014", "2015",  "2016",  "2017",  "2018",  "2019")) +
  labs(
    title    = "", 
    x        = "Year",
    y        = "Trust %",
    )

plot(g)
```

```{r}
g <- ggplot() +
  geom_line(data, mapping = aes(x = period, y = parliament_trust, group = Country, color = target)) +
  geom_vline(xintercept = 75, linetype = "dashed") +
  scale_color_manual(values = c("black", "grey")) +
  scale_x_continuous(breaks = seq(62,92,2),
                     labels=c("2004","2005", "2006", "2007", "2008",  "2009",  "2010",  "2011",  "2012",  "2013",  "2014", "2015",  "2016",  "2017",  "2018",  "2019")) +
  labs(
    title    = "", 
    x        = "Year",
    y        = "Trust %",
    )

plot(g)
```


## government trust
```{r}
# initial the synthetic control object
gov_trust_result <-
data %>%
synthetic_control(outcome = government_trust,
                  unit = Country,
                  time = period,
                  i_unit = "Italy",
                  i_time = 76,
                  generate_placebos= T) %>%

# Generate the aggregate predictors used to generate the weights
  generate_predictor(
                     Lib_Dem = mean(log_Liberal_Democracy_index, na.rm=TRUE),
                     Corruption =mean(log_Executive_corruption_index,na.rm=TRUE),
                     Egalitarian =mean(log_Egalitarian_Component_index,na.rm=TRUE),
                     Accountability= mean(log_Accountability_index,na.rm=TRUE),
                     Unemployment=mean(log_Unemployment_total_ILO_estimate,na.rm=TRUE),
                     GDP_Growth_p_C=mean( mm_Gdp_Growth_per_Capita,na.rm=TRUE),
                     trust_Gov=mean(government_trust,na.rm=TRUE),
                     trust_par =mean(parliament_trust ,na.rm=TRUE),
                     democracy_age = mean(log_tensys,na.rm=TRUE),
                     party_age = mean(log_partyage,na.rm=TRUE),
                     gov_seat= mean(mm_gov_support,na.rm=TRUE),
                     gov_vote=mean(mm_numvote, na.rm=TRUE),
                     current_account_balance= mean(BCA_NGDPD, na.rm = TRUE),
                     gov_net_lend_per =mean(log_GGXCNL_NGDP, na.rm=TRUE),
                     population =mean(log_LP, na.rm=TRUE),
                     investment= mean(NID_NGDP, na.rm = TRUE),
                     inflation= mean(PCPI, na.rm = TRUE),
                     import=mean(TM_RPCH, na.rm=TRUE),
                     export=mean(TX_RPCH, na.rm = TRUE)
                     )%>%
  
  # Generate the fitted weights for the synthetic control
  generate_weights(optimization_window =62:76, rm.na=TRUE) %>%

  # Generate the synthetic control
  generate_control()

```



```{r}
gov_trust_result %>% plot_trends() +
  # 表示する文字の設定
  labs(
    title    = "Synthetic and Observed Government Trust", 
    x        = "Year",
    y        = "Trust %",
    ) + 
  scale_x_continuous(breaks = seq(62,92,2),
                     labels=c("2004","2005", "2006", "2007", "2008",  "2009",  "2010",  "2011",  "2012",  "2013",  "2014", "2015",  "2016",  "2017",  "2018",  "2019"))
```
```{r}
gov_trust_result %>% plot_differences() +
  # 表示する文字の設定
  labs(
    x        = "Year",
    y        = "Diff of Trust %",
    ) + 
  scale_x_continuous(breaks = seq(62,92,2),
                     labels=c("2004","2005", "2006", "2007", "2008",  "2009",  "2010",  "2011",  "2012",  "2013",  "2014", "2015",  "2016",  "2017",  "2018",  "2019"))
```

```{r}
gov_trust_result %>% plot_weights()
```

```{r}
gov_trust_result %>% grab_balance_table()
```

```{r}
gov_trust_result %>% plot_mspe_ratio()
```
```{r}
gov_trust_result %>% grab_significance()
```

```{r}
gov_trust_result %>% grab_synthetic_control(placebo = T)
```

```{r}
gov_trust_result %>% plot_placebos()+
  # 表示する文字の設定
  labs(
    x        = "Year",
    y        = "Diff of Trust %",
    ) + 
  scale_x_continuous(breaks = seq(62,92,2),
                     labels=c("2004","2005", "2006", "2007", "2008",  "2009",  "2010",  "2011",  "2012",  "2013",  "2014", "2015",  "2016",  "2017",  "2018",  "2019"))
```


## gov_trust until 2013

```{r}
data2013 <- subset(data, subset=(data$period<80))
```

```{r}
gov_trust_result2013 <-
data2013 %>%
synthetic_control(outcome = government_trust,
                  unit = Country,
                  time = period,
                  i_unit = "Italy",
                  i_time = 76,
                  generate_placebos= T) %>%

# Generate the aggregate predictors used to generate the weights
  generate_predictor(
                     Lib_Dem = mean(log_Liberal_Democracy_index, na.rm=TRUE),
                     Corruption =mean(log_Executive_corruption_index,na.rm=TRUE),
                     Egalitarian =mean(log_Egalitarian_Component_index,na.rm=TRUE),
                     Accountability= mean(log_Accountability_index,na.rm=TRUE),
                     Unemployment=mean(log_Unemployment_total_ILO_estimate,na.rm=TRUE),
                     GDP_Growth_p_C=mean( mm_Gdp_Growth_per_Capita,na.rm=TRUE),
                     trust_Gov=mean(government_trust,na.rm=TRUE),
                     trust_par =mean(parliament_trust ,na.rm=TRUE),
                     democracy_age = mean(log_tensys,na.rm=TRUE),
                     party_age = mean(log_partyage,na.rm=TRUE),
                     gov_seat= mean(mm_gov_support,na.rm=TRUE),
                     gov_vote=mean(mm_numvote, na.rm=TRUE),
                     current_account_balance= mean(BCA_NGDPD, na.rm = TRUE),
                     gov_net_lend_per =mean(log_GGXCNL_NGDP, na.rm=TRUE),
                     population =mean(log_LP, na.rm=TRUE),
                     investment= mean(NID_NGDP, na.rm = TRUE),
                     inflation= mean(PCPI, na.rm = TRUE),
                     import=mean(TM_RPCH, na.rm=TRUE),
                     export=mean(TX_RPCH, na.rm = TRUE)
                     )%>%
  
  # Generate the fitted weights for the synthetic control
  generate_weights(optimization_window =62:76, rm.na=TRUE) %>%

  # Generate the synthetic control
  generate_control()
```




```{r}
gov_trust_result2013 %>% grab_significance()
```


```{r}
gov_trust_result2013 %>% plot_mspe_ratio()
```



## parliament trust

```{r}
# initial the synthetic control object
par_trust_result <-
data %>%
synthetic_control(outcome = parliament_trust,
                  unit = Country,
                  time = period,
                  i_unit = "Italy",
                  i_time = 76,
                  generate_placebos= T) %>%

# Generate the aggregate predictors used to generate the weights
  generate_predictor(
                     Lib_Dem = mean(log_Liberal_Democracy_index, na.rm=TRUE),
                     Corruption =mean(log_Executive_corruption_index,na.rm=TRUE),
                     Egalitarian =mean(log_Egalitarian_Component_index,na.rm=TRUE),
                     Accountability= mean(log_Accountability_index,na.rm=TRUE),
                     Unemployment=mean(log_Unemployment_total_ILO_estimate,na.rm=TRUE),
                     GDP_Growth_p_C=mean( mm_Gdp_Growth_per_Capita,na.rm=TRUE),
                     trust_Gov=mean(government_trust,na.rm=TRUE),
                     trust_par =mean(parliament_trust ,na.rm=TRUE),
                     democracy_age = mean(log_tensys,na.rm=TRUE),
                     party_age = mean(log_partyage,na.rm=TRUE),
                     gov_seat= mean(mm_gov_support,na.rm=TRUE),
                     gov_vote=mean(mm_numvote, na.rm=TRUE),
                     current_account_balance= mean(BCA_NGDPD, na.rm = TRUE),
                     gov_net_lend_per =mean(log_GGXCNL_NGDP, na.rm=TRUE),
                     population =mean(log_LP, na.rm=TRUE),
                     investment= mean(NID_NGDP, na.rm = TRUE),
                     inflation= mean(PCPI, na.rm = TRUE),
                     import=mean(TM_RPCH, na.rm=TRUE),
                     export=mean(TX_RPCH, na.rm = TRUE)
                     )%>%
  
  
  # Generate the fitted weights for the synthetic control
  generate_weights(optimization_window =62:76, rm.na=TRUE) %>%

  # Generate the synthetic control
  generate_control()

```


```{r}
par_trust_result %>% plot_trends() +
  # 表示する文字の設定
  labs(
    title    = "Synthetic and Observed Parliament Trust", 
    x        = "Year",
    y        = "Trust %",
    ) + 
  scale_x_continuous(breaks = seq(62,92,2),
                     labels=c("2004","2005", "2006", "2007", "2008",  "2009",  "2010",  "2011",  "2012",  "2013",  "2014", "2015",  "2016",  "2017",  "2018",  "2019"))
```
```{r}
par_trust_result %>% plot_differences() +
  # 表示する文字の設定
  labs(
    x        = "Year",
    y        = "Diff of Trust %",
    ) + 
  scale_x_continuous(breaks = seq(62,92,2),
                     labels=c("2004","2005", "2006", "2007", "2008",  "2009",  "2010",  "2011",  "2012",  "2013",  "2014", "2015",  "2016",  "2017",  "2018",  "2019"))
```

```{r}
par_trust_result %>% plot_weights()
```

```{r}
par_trust_result %>% grab_balance_table()
```

```{r}
par_trust_result %>% plot_mspe_ratio()
```
```{r}
par_trust_result %>% grab_significance()
```


```{r}
par_trust_result %>% grab_synthetic_control(placebo = T)
```
```{r}
par_trust_result %>% plot_placebos()+
  # 表示する文字の設定
  labs(
    x        = "Year",
    y        = "Diff of Trust %",
    ) + 
  scale_x_continuous(breaks = seq(62,92,2),
                     labels=c("2004","2005", "2006", "2007", "2008",  "2009",  "2010",  "2011",  "2012",  "2013",  "2014", "2015",  "2016",  "2017",  "2018",  "2019"))
```

## parliament trust until 2013

```{r}
par_trust_result2013 <-
data2013 %>%
synthetic_control(outcome = parliament_trust,
                  unit = Country,
                  time = period,
                  i_unit = "Italy",
                  i_time = 76,
                  generate_placebos= T) %>%
# Generate the aggregate predictors used to generate the weights
  generate_predictor(
                     Lib_Dem = mean(log_Liberal_Democracy_index, na.rm=TRUE),
                     Corruption =mean(log_Executive_corruption_index,na.rm=TRUE),
                     Egalitarian =mean(log_Egalitarian_Component_index,na.rm=TRUE),
                     Accountability= mean(log_Accountability_index,na.rm=TRUE),
                     Unemployment=mean(log_Unemployment_total_ILO_estimate,na.rm=TRUE),
                     GDP_Growth_p_C=mean( mm_Gdp_Growth_per_Capita,na.rm=TRUE),
                     trust_Gov=mean(government_trust,na.rm=TRUE),
                     trust_Par =mean(parliament_trust ,na.rm=TRUE),
                     democracy_age = mean(log_tensys,na.rm=TRUE),
                     party_age = mean(log_partyage,na.rm=TRUE),
                     gov_seat= mean(mm_gov_support,na.rm=TRUE),
                     gov_vote=mean(mm_numvote, na.rm=TRUE),
                     current_account_balance= mean(BCA_NGDPD, na.rm = TRUE),
                     gov_net_lend_per =mean(log_GGXCNL_NGDP, na.rm=TRUE),
                     population =mean(log_LP, na.rm=TRUE),
                     investment= mean(NID_NGDP, na.rm = TRUE),
                     inflation= mean(PCPI, na.rm = TRUE),
                     import=mean(TM_RPCH, na.rm=TRUE),
                     export=mean(TX_RPCH, na.rm = TRUE)
                     )%>%
  
  
  # Generate the fitted weights for the synthetic control
  generate_weights(optimization_window =62:76, rm.na=TRUE) %>%


  # Generate the synthetic control
  generate_control()


par_trust_result2013 %>% grab_significance()
```

```{r}
par_trust_result %>% plot_mspe_ratio()
```


## robustness check
### western europe


```{r}
we <- subset(data, subset=(data$Country=="Austria" |data$Country==        "Belgium"     |data$Country==    "Denmark"    |data$Country==     "France"    |data$Country==      "Finland"    |data$Country==     "Germany"  |data$Country==       "Greece"     |data$Country==    "Great Britain"  |data$Country==
 "Ireland"     |data$Country==    "Italy"  |data$Country==         "Luxembourg"|data$Country==      "The Netherlands" |data$Country=="Portugal"      |data$Country==  "Spain"    |data$Country==       "Sweden"   ))
```





```{r}
# initial the synthetic control object
gov_trust_result <-
we %>%
synthetic_control(outcome = government_trust,
                  unit = Country,
                  time = period,
                  i_unit = "Italy",
                  i_time = 76,
                  generate_placebos= T) %>%

# Generate the aggregate predictors used to generate the weights
  generate_predictor(
                     Lib_Dem = mean(log_Liberal_Democracy_index, na.rm=TRUE),
                     Corruption =mean(log_Executive_corruption_index,na.rm=TRUE),
                     Egalitarian =mean(log_Egalitarian_Component_index,na.rm=TRUE),
                     Accountability= mean(log_Accountability_index,na.rm=TRUE),
                     Unemployment=mean(log_Unemployment_total_ILO_estimate,na.rm=TRUE),
                     GDP_Growth_p_C=mean( mm_Gdp_Growth_per_Capita,na.rm=TRUE),
                     trust_Gov=mean(government_trust,na.rm=TRUE),
                     trust_par =mean(parliament_trust ,na.rm=TRUE),
                     democracy_age = mean(log_tensys,na.rm=TRUE),
                     party_age = mean(log_partyage,na.rm=TRUE),
                     gov_seat= mean(mm_gov_support,na.rm=TRUE),
                     gov_vote=mean(mm_numvote, na.rm=TRUE),
                     current_account_balance= mean(BCA_NGDPD, na.rm = TRUE),
                     gov_net_lend_per =mean(log_GGXCNL_NGDP, na.rm=TRUE),
                     population =mean(log_LP, na.rm=TRUE),
                     investment= mean(NID_NGDP, na.rm = TRUE),
                     inflation= mean(PCPI, na.rm = TRUE),
                     import=mean(TM_RPCH, na.rm=TRUE),
                     export=mean(TX_RPCH, na.rm = TRUE)
                     )%>%
  
  
  # Generate the fitted weights for the synthetic control
  generate_weights(optimization_window =62:76, rm.na=TRUE) %>%

  # Generate the synthetic control
  generate_control()

```



```{r}
gov_trust_result %>% plot_trends() +
  # 表示する文字の設定
  labs(
    title    = "Synthetic and Observed Government Trust", 
    x        = "Year",
    y        = "Trust %",
    ) + 
  scale_x_continuous(breaks = seq(62,92,2),
                     labels=c("2004","2005", "2006", "2007", "2008",  "2009",  "2010",  "2011",  "2012",  "2013",  "2014", "2015",  "2016",  "2017",  "2018",  "2019"))
```




```{r}
gov_trust_result %>% plot_differences() +
  # 表示する文字の設定
  labs(
    x        = "Year",
    y        = "Diff of Trust %",
    ) + 
  scale_x_continuous(breaks = seq(62,92,2),
                     labels=c("2004","2005", "2006", "2007", "2008",  "2009",  "2010",  "2011",  "2012",  "2013",  "2014", "2015",  "2016",  "2017",  "2018",  "2019"))
```

```{r}
gov_trust_result %>% plot_weights()
```

```{r}
gov_trust_result %>% grab_balance_table()
```


```{r}
gov_trust_result %>% plot_mspe_ratio()
```
```{r}
gov_trust_result %>% grab_significance()
```


```{r}
gov_trust_result %>% grab_synthetic_control(placebo = T)
```

```{r}
gov_trust_result %>% plot_placebos()
```



```{r}
# initial the synthetic control object
par_trust_result <-
data %>%
synthetic_control(outcome = parliament_trust,
                  unit = Country,
                  time = period,
                  i_unit = "Italy",
                  i_time = 76,
                  generate_placebos= T) %>%
# Generate the aggregate predictors used to generate the weights
  generate_predictor(
                     Lib_Dem = mean(log_Liberal_Democracy_index, na.rm=TRUE),
                     Corruption =mean(log_Executive_corruption_index,na.rm=TRUE),
                     Egalitarian =mean(log_Egalitarian_Component_index,na.rm=TRUE),
                     Accountability= mean(log_Accountability_index,na.rm=TRUE),
                     Unemployment=mean(log_Unemployment_total_ILO_estimate,na.rm=TRUE),
                     GDP_Growth_p_C=mean( mm_Gdp_Growth_per_Capita,na.rm=TRUE),
                     trust_Gov=mean(government_trust,na.rm=TRUE),
                     trust_par =mean(parliament_trust ,na.rm=TRUE),
                     democracy_age = mean(log_tensys,na.rm=TRUE),
                     party_age = mean(log_partyage,na.rm=TRUE),
                     gov_seat= mean(mm_gov_support,na.rm=TRUE),
                     gov_vote=mean(mm_numvote, na.rm=TRUE),
                     current_account_balance= mean(BCA_NGDPD, na.rm = TRUE),
                     gov_net_lend_per =mean(log_GGXCNL_NGDP, na.rm=TRUE),
                     population =mean(log_LP, na.rm=TRUE),
                     investment= mean(NID_NGDP, na.rm = TRUE),
                     inflation= mean(PCPI, na.rm = TRUE),
                     import=mean(TM_RPCH, na.rm=TRUE),
                     export=mean(TX_RPCH, na.rm = TRUE)
                     )%>%
  
  
  # Generate the fitted weights for the synthetic control
  generate_weights(optimization_window =62:76, rm.na=TRUE) %>%


  # Generate the synthetic control
  generate_control()

```


```{r}
par_trust_result %>% plot_trends() +
  # 表示する文字の設定
  labs(
    title    = "Synthetic and Observed Parliament Trust", 
    x        = "Year",
    y        = "Trust %",
    ) + 
  scale_x_continuous(breaks = seq(62,92,2),
                     labels=c("2004","2005", "2006", "2007", "2008",  "2009",  "2010",  "2011",  "2012",  "2013",  "2014", "2015",  "2016",  "2017",  "2018",  "2019"))
```
```{r}
par_trust_result %>% plot_differences() +
  # 表示する文字の設定
  labs(
    x        = "Year",
    y        = "Diff of Trust %",
    ) + 
  scale_x_continuous(breaks = seq(62,92,2),
                     labels=c("2004","2005", "2006", "2007", "2008",  "2009",  "2010",  "2011",  "2012",  "2013",  "2014", "2015",  "2016",  "2017",  "2018",  "2019"))
```

```{r}
par_trust_result %>% plot_weights()
```

```{r}
par_trust_result %>% grab_balance_table()
```

```{r}
par_trust_result %>% plot_mspe_ratio()
```
```{r}
par_trust_result %>% grab_significance()
```


```{r}
par_trust_result %>% grab_synthetic_control(placebo = T)
```
```{r}
par_trust_result %>% plot_placebos()
```


```{r}


ggplot(data, aes(x = period, y = parliament_trust, color = Country)) +
  geom_line() +
  labs(x = "Period", y = "Parliament Trust", color = "Country") +
  theme_minimal()


```


### PIGS
```{r}
pigs <- subset(data, subset = (data$Country== "Portugal"   |data$Country==     "Spain" |data$Country==   "Ireland"|data$Country== "Italy" |data$Country=="France"|data$Country== "Malta"|data$Country==  "Greece"  ))
```
```{r}
ggplot(pigs, aes(x = period, y = parliament_trust, color = Country)) +
  geom_line() +
  labs(x = "Period", y = "Parliament Trust", color = "Country") +
  theme_minimal()


```
```{r}
ggplot(pigs, aes(x = period, y = government_trust, color = Country)) +
  geom_line() +
  labs(x = "Period", y = "Government Trust", color = "Country") +
  theme_minimal()


```

